package service.array;



import dto.Roledto;
import entity.Role;
import service.IRoleService;
import service.IUserService;
import vo.Rolevo;
import vo.Result;

public class RoleService extends BaseService<Role, Roledto, Rolevo> implements IRoleService {

    private IUserService userService;

    public static IRoleService singleton;

    private RoleService(){
        data = new Role[DATA_SIZE + 1];
        temp = new Rolevo[DATA_SIZE];
    }

    public static IRoleService getInstance(){
        if(singleton == null){
            singleton = new RoleService();
        }
        return singleton;
    }

    @Override
    Rolevo convertToVo(Role role) {
        return new Rolevo(role.getId(), role.name, role.dataScope);
    }
    @Override
    Role createEntity(Roledto dto, int id) {
        return new Role(id,dto.name, dto.dataScope);
    }

    @Override
    void updateEntity(Roledto dto, Role role) {
        role.name  = dto.name;
        role.dataScope = dto.dataScope;
    }

    @Override
    public Result delete(int id){
        if(!existById(id)){
            return Result.failure("删除失败,不存在该数据");
        }
        if(userService == null){
            userService =UserService.getInstance();
        }
        if(userService.roleInUse(id)){
            return Result.failure("删除失败,还有用户具有该角色,不能删除");
        }
        data[id]=null;
        return Result.success("删除成功",null);
    }
}